/*
 * @Author: fishroom
 * @Date: 2022-10-26 15:02:07
 * @LastEditTime: 2022-11-28 14:11:52
 * @FilePath: \koa\app\util\captchaCode.ts
 */
import { Context } from "koa";
import svgCaptcha from "svg-captcha";
function setCaptchaCode(ctx: Context, options: any = undefined) {
  let obj: any = {
    size: 4, //验证码长度
    fontSize: 45, //验证码字号
    noise: Math.floor(Math.random() * 3) + 2, //干扰线条数目_随机0-5条
    width: 120, //宽度
    height: 40, //高度
    color: true, //验证码字符是否有颜色，默认是没有，但是如果设置了背景颜色，那么默认就是有字符颜色
    background: "#ccc", //背景色
  };
  for (let key in options) {
    obj[key] = options[key];
  }
  let captcha = svgCaptcha.create(obj);
  console.log("验证码",captcha.text);
  
  // @ts-ignore
  ctx.session.sessionId = captcha.text;
  // ctx.session.captcha = String(captcha.text);
  // 设置响应头
  ctx.response.type = "image/svg+xml";

  ctx.body = captcha.data;
}
export default { setCaptchaCode };
